home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / FontChooser.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  13.8 KB  |  395 lines  |  [TEXT/CWIE]

  1. // FontChooser.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9. // ALERT!!! - jla - need to think about where choosers appear onscreen
  10.  
  11. /** Object subclass creating a View with controls for selecting a Font.
  12.   * The Set button sends the command ExtendedTarget.SET_FONT to the first
  13.   * target in the Target Chain that can perform the command. The FontChooser
  14.   * can be added to the View hierarchy, or given a Window to display itself
  15.   * in, using the <b>setWindow()</b> method.
  16.   * @see RootView#showFontChooser
  17.   * @see ExtendedTarget
  18.   * @note 1.0 changes for keyboard UI
  19.   */
  20.  
  21. public class FontChooser implements Target {
  22.     ListView               _nameList;
  23.     Popup                  _sizePopup, _stylePopup;
  24.     TextField              _sizeTextField, _messageTextField;
  25.     Button                 _setButton;
  26.     Font                   _currentFont;
  27.     private ContainerView  contentView;
  28.     private Window         window;
  29.  
  30.     /* constructors */
  31.  
  32.     /** Constructs a FontChooser. */
  33.     public FontChooser() {
  34.         super();
  35.  
  36.         ListView        tmpList;
  37.         TextField       textField;
  38.         ScrollGroup     scrollGroup;
  39.         ContainerView   ruleView;
  40.         FontItem        newItem, proto;
  41.         Rect            listRect;
  42.         String          styleArray[] = new String[4];
  43.         int             styleTagArray[] = new int[4];
  44.         int             i, count, sizeArray[] = new int[6];
  45.  
  46.         contentView = new ContainerView(0, 0, 178, 120);
  47.  
  48.         contentView.setBackgroundColor(Color.lightGray);
  49.         contentView.setBorder(null);
  50.         contentView.setHorizResizeInstruction(View.WIDTH_CAN_CHANGE);
  51.         contentView.setVertResizeInstruction(View.HEIGHT_CAN_CHANGE);
  52.  
  53.         scrollGroup = new ScrollGroup(4, 19, 90, 61);
  54.         scrollGroup.setHasVertScrollBar(true);
  55.         scrollGroup.setBorder(BezelBorder.loweredBezel());
  56.         scrollGroup.setHorizResizeInstruction(View.WIDTH_CAN_CHANGE);
  57.         scrollGroup.setVertResizeInstruction(View.HEIGHT_CAN_CHANGE);
  58.  
  59.         listRect = scrollGroup.scrollView().bounds;
  60.         _nameList = new ListView(0, 0, listRect.width, listRect.width);
  61.         _nameList.setHorizResizeInstruction(View.WIDTH_CAN_CHANGE);
  62.         _nameList.setPrototypeItem(new FontItem());
  63.         _nameList.prototypeItem().setFont(Font.fontNamed("Default"));
  64.         _loadNameList();
  65.  
  66.         scrollGroup.setContentView(_nameList);
  67.         contentView.addSubview(scrollGroup);
  68.  
  69.         textField = new TextField(28, 1, 90 - 20, 18);
  70.         textField.setEditable(false);
  71.         textField.setTextColor(Color.black);
  72.         textField.setFont(Font.fontNamed("Helvetica",
  73.                                                             Font.BOLD, 12));
  74.         textField.setBackgroundColor(Color.lightGray);
  75.         textField.setStringValue("Name");
  76.         textField.setJustification(Graphics.LEFT_JUSTIFIED);
  77.         textField.setBorder(null);
  78.         textField.setHorizResizeInstruction(View.RIGHT_MARGIN_CAN_CHANGE);
  79.         textField.setVertResizeInstruction(View.BOTTOM_MARGIN_CAN_CHANGE);
  80.         contentView.addSubview(textField);
  81.  
  82.         /* size popup */
  83.         _sizePopup = new Popup(99, 19, 45, 20);
  84.         proto = new FontItem();
  85.         proto.setPopup(_sizePopup);
  86.         proto.setFont(Font.fontNamed("Default"));
  87.         _sizePopup.setPrototypeItem(proto);
  88.  
  89.         sizeArray[0] = 8;
  90.         sizeArray[1] = 10;
  91.         sizeArray[2] = 12;
  92.         sizeArray[3] = 14;
  93.         sizeArray[4] = 24;
  94.         sizeArray[5] = 36;
  95.         count = sizeArray.length;
  96.         tmpList = _sizePopup.popupList();
  97.         for (i = 0; i < count; i++) {
  98.             newItem = (FontItem)tmpList.addItem();
  99.             newItem.setTitle(Integer.toString(sizeArray[i]));
  100.             newItem.setTag(sizeArray[i]);
  101.         }
  102.         newItem = (FontItem)tmpList.addItem();
  103.         newItem.setTitle("Other");
  104.         newItem.setTag(-1);
  105.         _sizePopup.setTarget(this);
  106.         _sizePopup.setHorizResizeInstruction(View.LEFT_MARGIN_CAN_CHANGE);
  107.         _sizePopup.setVertResizeInstruction(View.BOTTOM_MARGIN_CAN_CHANGE);
  108.         contentView.addSubview(_sizePopup);
  109.  
  110.         _sizeTextField = new TextField(99 + 47, 19, 25, 20);
  111.         _sizeTextField.setEditable(true);
  112.         _sizeTextField.setContentsChangedCommandAndTarget("", this);
  113.         _sizeTextField.setHorizResizeInstruction(View.LEFT_MARGIN_CAN_CHANGE);
  114.         _sizeTextField.setVertResizeInstruction(View.BOTTOM_MARGIN_CAN_CHANGE);
  115.         contentView.addSubview(_sizeTextField);
  116.  
  117.         textField = new TextField(100, 1, 45 - 15, 18);
  118.         textField.setEditable(false);
  119.         textField.setTextColor(Color.black);
  120.         textField.setBackgroundColor(Color.lightGray);
  121.         textField.setFont(Font.fontNamed("Helvetica", Font.BOLD, 12));
  122.         textField.setStringValue("Size");
  123.         textField.setJustification(Graphics.LEFT_JUSTIFIED);
  124.         textField.setBorder(null);
  125.         textField.setHorizResizeInstruction(View.LEFT_MARGIN_CAN_CHANGE);
  126.         textField.setVertResizeInstruction(View.BOTTOM_MARGIN_CAN_CHANGE);
  127.         contentView.addSubview(textField);
  128.  
  129.         /* style popup */
  130.         _stylePopup = new Popup(99, 61, 75, 21);
  131.         proto = new FontItem();
  132.         proto.setPopup(_stylePopup);
  133.         proto.setFont(Font.fontNamed("Default"));
  134.         _stylePopup.setPrototypeItem(proto);
  135.  
  136.         styleArray[0] = "Plain";
  137.         styleArray[1] = "Bold";
  138.         styleArray[2] = "Italic";
  139.         styleArray[3] = "Bold Italic";
  140.         styleTagArray[0] = Font.PLAIN;
  141.         styleTagArray[1] = Font.BOLD;
  142.         styleTagArray[2] = Font.ITALIC;
  143.         styleTagArray[3] = Font.BOLD | Font.ITALIC;
  144.         count = styleArray.length;
  145.         tmpList = _stylePopup.popupList();
  146.         for (i = 0; i < count; i++) {
  147.             newItem = (FontItem)tmpList.addItem();
  148.             newItem.setTitle(styleArray[i]);
  149.             newItem.setTag(styleTagArray[i]);
  150.         }
  151.         _stylePopup.setHorizResizeInstruction(View.LEFT_MARGIN_CAN_CHANGE);
  152.         _stylePopup.setVertResizeInstruction(View.BOTTOM_MARGIN_CAN_CHANGE);
  153.         contentView.addSubview(_stylePopup);
  154.  
  155.         textField = new TextField(100, 43, 45 - 15, 18);
  156.         textField.setEditable(false);
  157.         textField.setTextColor(Color.black);
  158.         textField.setBackgroundColor(Color.lightGray);
  159.         textField.setFont(Font.fontNamed("Helvetica",
  160.                                                             Font.BOLD, 12));
  161.         textField.setStringValue("Style");
  162.         textField.setJustification(Graphics.LEFT_JUSTIFIED);
  163.         textField.setBorder(null);
  164.         textField.setHorizResizeInstruction(View.LEFT_MARGIN_CAN_CHANGE);
  165.         textField.setVertResizeInstruction(View.BOTTOM_MARGIN_CAN_CHANGE);
  166.         contentView.addSubview(textField);
  167.  
  168.         /* rule */
  169.         ruleView = new ContainerView(-2, 87, 184, 2);
  170.         ruleView.setHorizResizeInstruction(View.WIDTH_CAN_CHANGE);
  171.         ruleView.setVertResizeInstruction(View.TOP_MARGIN_CAN_CHANGE);
  172.         contentView.addSubview(ruleView);
  173.  
  174.         _setButton = new Button(124, 95, 50, 21);
  175.         _setButton.setTitle("Set");
  176.         _setButton.setHorizResizeInstruction(View.LEFT_MARGIN_CAN_CHANGE);
  177.         _setButton.setVertResizeInstruction(View.TOP_MARGIN_CAN_CHANGE);
  178.         _setButton.setCommand(ExtendedTarget.SET_FONT);
  179.         _setButton.setTarget(this);
  180.         contentView.addSubview(_setButton);
  181.  
  182.         _messageTextField = new TextField(4, 95, 100, 21);
  183.         _messageTextField.setEditable(false);
  184.         _messageTextField.setBorder(null);
  185.         _messageTextField.setTextColor(Color.gray);
  186.         _messageTextField.setBackgroundColor(Color.lightGray);
  187.         _messageTextField.setFont(
  188.                     Font.fontNamed("Helvetica",
  189.                                                         Font.PLAIN, 10));
  190.         _messageTextField.setHorizResizeInstruction(
  191.                                             View.RIGHT_MARGIN_CAN_CHANGE);
  192.         _messageTextField.setVertResizeInstruction(View.TOP_MARGIN_CAN_CHANGE);
  193.         contentView.addSubview(_messageTextField);
  194.  
  195.         setFont(Font.defaultFont());
  196.     }
  197.  
  198.     private void _loadNameList() {
  199.         FontItem        newItem;
  200.         String          nameArray[] = new String[6],
  201.                         fontNameArray[] = new String[6];
  202.         int             i, count;
  203.  
  204.         nameArray[0] = "Courier";
  205.         nameArray[1] = "Dialog";
  206.         nameArray[2] = "Dialog Input";
  207.         nameArray[3] = "Helvetica";
  208.         nameArray[4] = "Times Roman";
  209.         nameArray[5] = "Zapf Dingbats";
  210.  
  211.         fontNameArray[0] = "Courier";
  212.         fontNameArray[1] = "Dialog";
  213.         fontNameArray[2] = "DialogInput";
  214.         fontNameArray[3] = "Helvetica";
  215.         fontNameArray[4] = "TimesRoman";
  216.         fontNameArray[5] = "ZapfDingbats";
  217.         count = nameArray.length;
  218.         for (i = 0; i < count; i++) {
  219.             newItem = (FontItem)_nameList.addItem();
  220.             newItem.setTitle(nameArray[i]);
  221.             newItem.setFontName(fontNameArray[i]);
  222.         }
  223.  
  224.         _nameList.setRowHeight(_nameList.minItemHeight());
  225.         _nameList.sizeToMinSize();
  226.     }
  227.  
  228.     /** If the FontChooser is in a Window, makes the Window visible.  You must
  229.       * have first set the FontChooser's Window using its <b>setWindow()</b>
  230.       * method.
  231.       * @see #setWindow
  232.       * @see RootView#showFontChooser
  233.       */
  234.     public void show() {
  235.         if (window != null)
  236.             window.show();
  237.     }
  238.  
  239.     /** Hides the FontChooser's Window.
  240.       * @see #show
  241.       */
  242.     public void hide() {
  243.         if (window != null)
  244.             window.hide();
  245.     }
  246.  
  247.     private void _setSizePopupToSize(int aSize) {
  248.         FontItem    nextItem;
  249.         int     i;
  250.  
  251.         i = _sizePopup.count();
  252.         while (i-- > 0) {
  253.             nextItem =(FontItem)_sizePopup.popupList().itemAt(i);
  254.             if (nextItem.tag() == aSize) {
  255.                 _sizePopup.selectItemAt(i);
  256.                 return;
  257.             }
  258.         }
  259.         if (i == -1) {
  260.             _sizePopup.selectItemAt(_sizePopup.count() - 1);
  261.         }
  262.     }
  263.  
  264.     /** Makes <b>aFont</b> the FontChooser's current font.
  265.       */
  266.     public void setFont(Font aFont) {
  267.         FontItem        nextFontItem;
  268.         FontItem            nextItem;
  269.         int             i;
  270.  
  271.         if (aFont == null) {
  272.             return;
  273.         }
  274.  
  275.         _currentFont = aFont;
  276.  
  277.         /* name */
  278.         i = _nameList.count();
  279.         while (i-- > 0) {
  280.             nextFontItem = (FontItem)_nameList.itemAt(i);
  281.             if (nextFontItem.hasFontName(aFont.family())) {
  282.                 _nameList.selectItemAt(i);
  283.                 _nameList.scrollItemAtToVisible(i);
  284.                 break;
  285.             }
  286.         }
  287.         if (i == -1) {
  288.             _nameList.selectItemAt(0);
  289.         }
  290.  
  291.         /* style */
  292.         if (aFont.isBold()) {
  293.             if (aFont.isItalic()) {
  294.                 _stylePopup.selectItemAt(3);
  295.             } else {
  296.                 _stylePopup.selectItemAt(1);
  297.             }
  298.         } else if (aFont.isItalic()) {
  299.             _stylePopup.selectItemAt(2);
  300.         } else {
  301.             _stylePopup.selectItemAt(0);
  302.         }
  303.  
  304.         /* size */
  305.         _setSizePopupToSize(aFont.size());
  306.         _sizeTextField.setIntValue(aFont.size());
  307.     }
  308.  
  309.     /** Returns the FontChooser's current font.
  310.       * @see #setFont
  311.       */
  312.     public Font font() {
  313.         Font            newFont;
  314.         FontItem        Item;
  315.         String          name;
  316.  
  317.         if (_nameList.selectedItem() == null) {
  318.             name = "";
  319.         } else {
  320.             Item = (FontItem)_nameList.selectedItem();
  321.             name = Item.fontName();
  322.         }
  323.  
  324.         newFont = Font.fontNamed(name,
  325.                                  ((FontItem)_stylePopup.selectedItem()).tag(),
  326.                                  _sizeTextField.intValue());
  327.  
  328.         return newFont;
  329.     }
  330.  
  331.     /** Performs the commands necessary for the FontChooser to operate. You
  332.       * should never call this method.
  333.       */
  334.     public void performCommand(String command, Object data) {
  335.         int             fontSize;
  336.  
  337.         if (data == _sizeTextField) {
  338.             fontSize = _sizeTextField.intValue();
  339.             if (fontSize > 0) {
  340.                 _setSizePopupToSize(fontSize);
  341.             } else {
  342.                 _setSizePopupToSize(8);
  343.                 _sizeTextField.setIntValue(8);
  344.             }
  345.         } else if (data == _sizePopup) {
  346.             fontSize = ((FontItem)(_sizePopup.selectedItem())).tag();
  347.             if (fontSize > 0) {
  348.                 _sizeTextField.setIntValue(fontSize);
  349.             }
  350.         } else if (data == _setButton) {
  351.             Target target = TargetChain.applicationChain();
  352.  
  353.             target.performCommand(command, font());
  354.         }
  355.     }
  356.  
  357.     /** Places the FontChooser in the Window <b>aWindow</b>. This method sizes
  358.       * <b>aWindow</b> to the minimum size necessary to display the
  359.       * FontChooser, as well as makes <b>aWindow</b> closable and sets its
  360.       * title.
  361.       */
  362.     public void setWindow(Window aWindow) {
  363.         Size    windowSize;
  364.         Rect    bounds;
  365.  
  366.         this.window = aWindow;
  367.         windowSize
  368.             = this.window.windowSizeForContentSize(contentView.width(),
  369.                                                    contentView.height());
  370.         this.window.sizeTo(windowSize.width, windowSize.height);
  371.         this.window.addSubview(contentView);
  372.         this.window.setTitle("Font Chooser");
  373.         bounds = this.window.bounds();
  374.         this.window.setMinSize(bounds.width, bounds.width);
  375.         if (this.window instanceof InternalWindow) {
  376.             InternalWindow iWindow = (InternalWindow) this.window;
  377.             iWindow.setCloseable(true);
  378.             iWindow.setBuffered(true);
  379.         }
  380.         this.window.setContainsDocument(false);
  381.     }
  382.  
  383.     /** Returns the FontChooser's Window.
  384.       * @see #setWindow
  385.       */
  386.     public Window window() {
  387.         return window;
  388.     }
  389.  
  390.     /** Returns the FontChooser's content View. */
  391.     public View contentView() {
  392.         return contentView;
  393.     }
  394. }
  395.